package pl.smath.renderer.utils;

import java.util.Vector;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class InteractionManager implements InteractionSocket {

	private Vector<Point> positions;
	private Vector<TextBox> gaps;
	private AbsolutePanel container;
	private AbsolutePanel canvasLayer;
	private AbsolutePanel aboveLayer;
	private TextBox testTextBox;
	
	public InteractionManager(Panel parent){
		container = new AbsolutePanel();
		container.setStyleName("smath-container");
		canvasLayer = new AbsolutePanel();
		canvasLayer.setStyleName("smath-canvas");
		aboveLayer = new AbsolutePanel();
		aboveLayer.setStyleName("smath-above");
		
		container.add(canvasLayer, 0, 0);
		container.add(aboveLayer, 0, 0);
		
		parent.add(container);

		testTextBox = new TextBox();
		testTextBox.setStyleName("smath-gap");
		aboveLayer.add(testTextBox, 0, 0);
		
		
		gaps = new Vector<TextBox>();
		positions = new Vector<Point>();
		
	}
	
	@Override
	public void registerElement(double x, double y) {
		positions.add(new Point(x, y));
	}
	
	public void process(){
		for (Point p : positions){
			TextBox tb = new TextBox();
			tb.setStyleName("smath-gap");
			gaps.add(tb);
			aboveLayer.add(tb, (int)p.getX(), (int)p.getY());
		}
	}

	@Override
	public void setCanvas(Widget canvas, int width, int height) {
		container.setSize(String.valueOf(width), String.valueOf(height));
		canvasLayer.setSize(String.valueOf(width), String.valueOf(height));
		aboveLayer.setSize(String.valueOf(width), String.valueOf(height));
		canvasLayer.add(canvas, 0, 0);
		
	}

	public int getTextBoxWidth(){
		return testTextBox.getOffsetWidth();
	}

	public int getTextBoxHeight(){
		return testTextBox.getOffsetHeight();
	}

	@Override
	public void removeTextBox() {
		aboveLayer.remove(testTextBox);
	}
	
	public int getGapsCount(){
		return gaps.size();
	}
	
	public TextBox getGapAt(int i){
		if (i < gaps.size())
			return gaps.get(i);
		return null;
	}
	
}
